package PartEdgeDemo;

import java.util.Random;
import java.util.*;

import y.base.Graph;
import y.base.Node;
import y.base.NodeCursor;
import y.view.Graph2D;

public class RegularCreater {
	  final int n;
	  final Graph2D g;
	  
	  private Random r;

	  public RegularCreater(int n, Graph2D g) {
		if (n > 1){this.n = n;}else{this.n = 1;}
	    this.g = g;
	    r = new Random();
	  }

	  public Graph2D create(){
		  
		// Funktioniert so noch nicht!!!!
		// TODO
		  
	    g.clear();

	    for(int i=0;i<n;i++){
	    	g.createNode(0,0,String.valueOf(i+1));
	    }//String.valueOf(g.nodeCount() + 1)

	    Node[] nodearray = g.getNodeArray();
	    List<Node> nodelist = new ArrayList<Node>();
	    int length = nodearray.length;
	    for (int i=0; i<length; i++)
	    	nodelist.add(nodearray[i]);
	    
	    Node currentNode = nodelist.remove(0);
	    while (currentNode != null){
	    	for (int i=currentNode.degree(); i<4; i++){
	    		Node v = nodelist.get(r.nextInt(nodelist.size()));
	    		if (v != null){
		    		g.createEdge(currentNode, v);
		    		if (v.degree() == 4){nodelist.remove(v);}
	    		}else{
	    			i = i-1;
	    		}
	    	}
	    	if (nodelist.size()>0){ //??
	    		currentNode = nodelist.remove(0);
	    	}else{
	    		currentNode = null;
	    	}
	    }
	    System.out.println(g);
	
	    return g;
	  }

}
